//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using EntLibContrib.Resource.Instrumentation;
using EntLibContrib.Resource.Managers;

namespace EntLibContrib.Resource.Configuration
{
	/// <summary>
	/// Helper utility for assembler and policy creators
	/// </summary>
	public static class ResourceManagerAssemblerHelper
	{
		#region Public Static Methods
		/// <summary>
		/// Made public for testing purposes.
		/// </summary>
		/// <param name="resourceManagerName">Name of the resource manager.</param>
		/// <param name="resourceProvider">The resource provider.</param>
		/// <param name="instrumentationProvider">The instrumentation provider.</param>
		/// <returns></returns>
		public static ResourceManager BuildResourceManager (string resourceManagerName,
																												IResourceProvider resourceProvider,
																												ResourceInstrumentationProvider instrumentationProvider)
		{
			ExtendedComponentResourceManager resourceManager = resourceProvider.CreateResourceManager();
			resourceManager.InstrumentationProvider = instrumentationProvider;
			return new ResourceManager(resourceManagerName, resourceManager);
		}

		/// <summary>
		/// Creates the resource provider from configuration.
		/// </summary>
		/// <param name="name">The resource provider instance name.</param>
		/// <param name="configurationSource">The configuration source.</param>
		/// <returns>
		/// the specified <see cref="IResourceProvider"/> from configuration
		/// </returns>
		public static IResourceProvider CreateResourceProvider(string name,
																													IConfigurationSource configurationSource)
		{
			if (string.IsNullOrEmpty(name))
				throw new ArgumentNullException("name");

			ResourceProviderFactory factory = new ResourceProviderFactory(configurationSource);
			return factory.Create(name);
		}

		/// <summary>
		/// Creates an instrumentation provider.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="configurationSource">The configuration source.</param>
		/// <param name="reflectionCache">The reflection cache.</param>
		/// <returns></returns>
		public static ResourceInstrumentationProvider CreateInstrumentationProvider(string name,
																																								IConfigurationSource configurationSource,
																																								ConfigurationReflectionCache reflectionCache)
		{
			ResourceInstrumentationProvider instrumentationProvider = new ResourceInstrumentationProvider();
			new InstrumentationAttachmentStrategy().AttachInstrumentation(name, instrumentationProvider, configurationSource, reflectionCache);

			return instrumentationProvider;
		}
		#endregion
	}
}